package com.hema.auction.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.bean.AddressInfo;
import com.hema.auction.bean.GoodsInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.GlideUtils;
import com.hema.auction.utils.Utils;
import com.hema.auction.utils.manager.SPManager;
import com.hema.auction.widget.view.TitleView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 描述:订单详情
 * 作者:john
 */
public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_market_price)
    TextView tvMarketPrice;
    @BindView(R.id.tv_action)
    TextView tvAction;
    @BindView(R.id.tv_auction_price_title)
    TextView tvAuctionPriceTitle;
    @BindView(R.id.tv_auction_price)
    TextView tvAuctionPrice;
    @BindView(R.id.tv_dealer_price_title)
    TextView tvDealerPriceTitle;
    @BindView(R.id.tv_dealer_price)
    TextView tvDealerPrice;
    @BindView(R.id.ll_address)
    View llAddress;
    @BindView(R.id.tv_address_tips)
    View tvAddressTips;
    @BindView(R.id.cx_line)
    View cxLine;
    private GoodsInfo info;
    private String status;
    private boolean isCjBuy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        info = getIntent().getParcelableExtra(Constant.EXTRA_GOOD_INFO);
        status = getIntent().getStringExtra(Constant.EXTRA_STATUS);
        isCjBuy = getIntent().getBooleanExtra(Constant.EXTRA_CJ_BUY, false);
        init();
    }

    private void init() {
        tvStatus.setText(status);
        tvMarketPrice.setText(Utils.priceUnitFormat(this, info.getPrice()));
        GlideUtils.load(this, info.getImg(), ivImg);
        showProgressDialog();
        HttpManager.getInstance(this).orderDetail(info.getOrderId(), this);
        setOrderStatus();
        if (isCjBuy) {
            /**
             * 差价购买的订单详情
             */
            tvAuctionPriceTitle.setText(R.string.use_shopping_coin);
            tvAuctionPrice.setText("-" + Utils.priceUnitFormat(this, info.getGwPrice()));//使用购物币
            tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getPrice())); //实付价格
            tvDealerPriceTitle.setText(R.string.paid_price_format);
            cxLine.setVisibility(View.VISIBLE);

        }
    }

    private void setOrderStatus() {
        if (SPManager.getInstance(this).getUserId().equals(info.getUid())) {
            //我拍中的
            switch (info.getStatus()) {
                //订单状态  订单状态  0->未付款，1->已付款(待发货)，2->已发货(待签收)，3->已收货(待晒单)，4->已过期，5->已取消,6未竞拍结束
                case 0:
                    tvStatus.setText("待付款");

                    tvAction.setText("立即付款");
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText(getString(R.string.dealer));
                    tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getOwnerName()));
                    break;
                case 1:
                    tvStatus.setText("已付款");

                    tvAction.setVisibility(View.GONE);
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText(getString(R.string.final_price_tip));
                    tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));
                    break;
                case 2:
                    tvStatus.setText("已发货");

                    tvAction.setText("确认签收");
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText(getString(R.string.final_price_tip));
                    tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));
                    break;
                case 3:
                    tvStatus.setText("已签收");

                    tvAction.setText("我要晒单");
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText("成交人:");
                    tvDealerPrice.setText(info.getOwnerName());

                    break;
                case 4:
                    tvStatus.setText("已过期");
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText(getString(R.string.final_price_tip));
                    tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));
                    //按钮隐藏
                    tvAction.setVisibility(View.GONE);
                    break;
                case 5:
                    tvStatus.setText("已取消");
                    tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                    tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));

                    tvDealerPriceTitle.setText(getString(R.string.final_price_tip));
                    tvDealerPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));
                    tvAction.setVisibility(View.GONE);
                    break;
                case 6:
                    if (info.getUid().equals("0")) {
                        tvStatus.setText("进行中");
                        tvAction.setText("继续竞拍");
                        tvAuctionPriceTitle.setText(getString(R.string.auction_price_tip));
                        tvAuctionPrice.setText(Utils.priceUnitFormat(this, info.getFinalPrice()));
                        tvDealerPriceTitle.setText(getString(R.string.i_bid_tip));
                        tvDealerPrice.setText(info.getOrderCount() + "次");
                    }
                    break;

            }
        }
    }

    @OnClick({R.id.tv_action, R.id.tv_address_tips, R.id.ll_address})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_action:
                String text = ((TextView) v).getText().toString();
                switch (text) {
                    case "立即付款":
                        break;
                    case "确认签收":
                        showProgressDialog();
                        HttpManager.getInstance(this).sureOrder(info.getOrderId(), this);
                        break;
                    case "我要晒单":
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(Constant.EXTRA_GOOD_INFO, info);
                        intentTo(PulishSunActivity.class, bundle);
                        break;
                    case "继续竞拍":
                        bundle = new Bundle();
                        bundle.putParcelable(Constant.EXTRA_GOOD_INFO, info);
                        intentTo(GoodsDetailsActivity.class, bundle);
                        break;

                }
                break;
            case R.id.tv_address_tips:
                if (info.getStatus() == 0)
                    intentToForResult(AddressActivity.class, Constant.REQUEST_CODE_ADDRESS);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE_ADDRESS && resultCode == RESULT_OK) {
            AddressInfo info = data.getParcelableExtra(Constant.EXTRA_ADDRESS_INFO);
            if (info != null) {
                tvName.setText(getString(R.string.addressee, info.getName()));
                tvPhone.setText(info.getPhone());
                tvAddress.setText(getString(R.string.delivery_address, info.getAddress()));
                tvAddressTips.setVisibility(View.GONE);
                llAddress.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_ORDER_DETAIL:
                try {
                    if (result.getInt("errcode") == 0) {
                        JSONObject data = result.getJSONObject("data");
                        if (Utils.isEmpty(data.getString("address"))) {
                            tvAddressTips.setVisibility(View.VISIBLE);
                            llAddress.setVisibility(View.GONE);
                        } else {
                            tvAddressTips.setVisibility(View.GONE);
                            llAddress.setVisibility(View.VISIBLE);
                        }
                        tvName.setText(getString(R.string.addressee, data.getString("user_name")));
                        tvPhone.setText(data.getString("c_phone"));
                        tvAddress.setText(getString(R.string.delivery_address, data.getString("address")));
                    } else {
                        titleView.setRightText("获取收货信息");
                    }
//                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                    titleView.setRightText("获取收货信息");
                }
                break;
            case TAG_SURE_ORDER:
                try {
                    if (result.getInt("errcode") == 0) {

                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
